package bd;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import model.AcaoForm;
import model.PerfilForm;
import model.SetorForm;
import model.StatusForm;

public class JDBCPerfilDAO implements StatusDAO, PerfilDAO
{
	Connection conn;
	public Vector searchallStatus() 
	{
		StatusForm status = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<StatusForm> vecStatus = new Vector<StatusForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select * from TAB_STATUS";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				status = new StatusForm(rs.getInt("COD_STATUS"), rs.getString("STR_DESCRICAO_STATUS"));
				System.out.println("Entrei no wile do status");
				System.out.println(strSQL);
				System.out.println(status.getDescricao_status());
				vecStatus.add(status);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecStatus;
	}
	
	
	/**
	 * Aqui
	 */
	
	
	
	public void excluirSetor(SetorForm set) 
	{
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try {
			stm = conn.createStatement();
			strSQL = "update TAB_SETOR set STR_DESABILITADO = '"+ set.getDesabilitado() +"'" +
					"where COD_SETOR = '"+ set.getCod_setor() +"'";	
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	public PerfilForm alterarPerfil(PerfilForm set)
	{
		PerfilForm perfil = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Statement stm2;
		try {
			stm = conn.createStatement();
			stm2 = conn.createStatement();
			strSQL = "update TAB_PERFIL set STR_NOME_PERFIL = '"+ set.getNome_perfil() +"'," +
					" STR_DESCRICAO_PERFIL = '"+ set.getDescricao_perfil() +"', STR_PERFIL_DESABILITADO = '"+ set.getPerfil_desabilitado() +"'" +
					"where COD_PERFIL = '"+ set.getCod_perfil() +"'";
			
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	strSQL = "select COD_PERFIL, STR_NOME_PERFIL, STR_DESCRICAO_PERFIL, STR_PERFIL_DESABILITADO from TAB_PERFIL where COD_PERFIL = '"+set.getCod_perfil()+"'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm2.executeQuery(strSQL);
			while (rs.next())
			{
				perfil = new PerfilForm(rs.getInt("COD_PERFIL"), rs.getString("STR_NOME_PERFIL"), rs.getString("STR_DESCRICAO_PERFIL"), rs.getString("STR_PERFIL_DESABILITADO"));
				System.out.println(strSQL);
			}
			rs.close();
			stm2.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return perfil;
	}
	public void excluirPerfil(PerfilForm set)
	{
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try {
			stm = conn.createStatement();
			strSQL = "update TAB_PERFIL set STR_PERFIL_DESABILITADO = '"+ set.getPerfil_desabilitado() +"'" +
					"where COD_PERFIL = '"+ set.getCod_perfil() +"'";	
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}
	public PerfilForm findPerfil(String parameter)
	{
		PerfilForm perfil = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try
		{
			stm = conn.createStatement();
			strSQL = "select COD_PERFIL, STR_NOME_PERFIL, STR_DESCRICAO_PERFIL, STR_PERFIL_DESABILITADO from TAB_PERFIL where COD_PERFIL = '"+parameter+"' and STR_PERFIL_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				perfil = new PerfilForm(rs.getInt("COD_PERFIL"), rs.getString("STR_NOME_PERFIL"), rs.getString("STR_DESCRICAO_PERFIL"), rs.getString("STR_PERFIL_DESABILITADO"));
				System.out.println(strSQL);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		
		return perfil;
	}
	public PerfilForm findexistentePerfil(String nome_Perfil)
	{
		PerfilForm perfil = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		
		try
		{
			stm = conn.createStatement();
			strSQL = "select COD_PERFIL, STR_NOME_PERFIL, STR_DESCRICAO_PERFIL, STR_PERFIL_DESABILITADO from TAB_PERFIL where STR_NOME_PERFIL = '"+nome_Perfil+"' and STR_PERFIL_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				perfil = new PerfilForm(rs.getInt("COD_PERFIL"), rs.getString("STR_NOME_PERFIL"), rs.getString("STR_DESCRICAO_PERFIL"), rs.getString("STR_PERFIL_DESABLITADO"));
				System.out.println("Entrei no wile");
				System.out.println(strSQL);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}	
		return perfil;
	}
	public PerfilForm insertPerfil(PerfilForm set)
	{
		PerfilForm perfil = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Statement stm2;
		try {
			stm = conn.createStatement();
			stm2 = conn.createStatement();
			strSQL = "insert into TAB_PERFIL (STR_NOME_PERFIL, STR_DESCRICAO_PERFIL, STR_PERFIL_DESABILITADO) " +
					" values (" +
					"'"+ set.getNome_perfil() +"', '"+ set.getDescricao_perfil() +"', '"+ set.getPerfil_desabilitado() +"')";
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	strSQL = "select COD_PERFIL, STR_NOME_PERFIL, STR_DESCRICAO_PERFIL, STR_PERFIL_DESABILITADO from TAB_PERFIL where STR_NOME_PERFIL = '"+set.getNome_perfil()+"'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm2.executeQuery(strSQL);
			while (rs.next())
			{
				perfil = new PerfilForm(rs.getInt("COD_PERFIL"), rs.getString("STR_NOME_PERFIL"), rs.getString("STR_DESCRICAO_PERFIL"), rs.getString("STR_PERFIL_DESABILITADO"));
				System.out.println(strSQL);
			}
			rs.close();
			stm2.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return perfil;
	}
	public Vector searchallperfil()
	{
		PerfilForm perfil = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<PerfilForm> vecPerfil = new Vector<PerfilForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select * from TAB_PERFIL where STR_PERFIL_DESABILITADO = 'FALSE'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				perfil = new PerfilForm(rs.getInt("COD_PERFIL"), rs.getString("STR_NOME_PERFIL"), rs.getString("STR_DESCRICAO_PERFIL"), rs.getString("STR_PERFIL_DESABILITADO"));
				System.out.println(strSQL);
				System.out.println(perfil.getNome_perfil());
				vecPerfil.add(perfil);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecPerfil;
	}
	public Vector searchallacao()
	{
		AcaoForm acao = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<AcaoForm> vecAcao = new Vector<AcaoForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select * from TAB_ACAO";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				acao = new AcaoForm(rs.getInt("COD_ACAO"), rs.getString("STR_NOME_ACAO"), rs.getString("ACTION"), rs.getString("KEY"), rs.getString("CONTROLE"));
				acao.setEditar("");
				System.out.println(strSQL);
				vecAcao.add(acao);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecAcao;
	}
	public void addacaoperfil(int cod_perfil, Integer cod_acao)
	{
		
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		
		try {
			stm = conn.createStatement();
		
			strSQL = "insert into TAB_PERFIL_ACAO (COD_PERFIL, COD_ACAO) " +
					" values (" +
					"'"+ cod_perfil +"', '"+ cod_acao +"')";
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	
		} catch (Exception e) 
		{
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	public Vector searchallmenu(int cod_perfil)
	{
		AcaoForm acao = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		Vector<AcaoForm> vecMenu = new Vector<AcaoForm>();
		try
		{
			stm = conn.createStatement();
			strSQL = "select ta.COD_ACAO, ta.STR_NOME_ACAO, ta.ACTION, ta.KEY, ta.CONTROLE from TAB_PERFIL_ACAO pa, TAB_ACAO ta where pa.COD_PERFIL = '"+cod_perfil+"' and pa.COD_ACAO = ta.COD_ACAO";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				acao = new AcaoForm(rs.getInt("COD_ACAO"), rs.getString("STR_NOME_ACAO"), rs.getString("ACTION"), rs.getString("KEY"), rs.getString("CONTROLE"));
				System.out.println("Entrei no wile do status");
				System.out.println(strSQL);
				vecMenu.add(acao);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return vecMenu;
	}


	public Vector getacaoperfil(int cod_perfil)
	{
		// TODO Auto-generated method stub
		return null;
	}


	public void deleteperfilacao(int cod_perfil)
	{
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		
		try {
			stm = conn.createStatement();
		
			strSQL = "delete from TAB_PERFIL_ACAO where COD_PERFIL = '"+ cod_perfil +"' "; 
					
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
	    	
		} catch (Exception e) 
		{
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}


	public AcaoForm getunicaacao(String temp)
	{
		AcaoForm acao = null;
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try
		{
			stm = conn.createStatement();
			strSQL = "select COD_ACAO, STR_NOME_ACAO, ACTION, KEY, CONTROLE from TAB_ACAO where CONTROLE = '"+temp+"'";
			ResultSet rs;
			System.out.println(strSQL);
			rs = stm.executeQuery(strSQL);
			while (rs.next())
			{
				acao = new AcaoForm(rs.getInt("COD_ACAO"), rs.getString("STR_NOME_ACAO"), rs.getString("ACTION"), rs.getString("KEY"), rs.getString("CONTROLE"));
				System.out.println(strSQL);
			}
			rs.close();
			stm.close();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		
		return acao;
	}


	public void alteraPerfilUsuario(int cod_perfil, int cod_perfil2)
	{
		conn = conexaoJDBC.getConnection();
		String strSQL = "";
		Statement stm;
		try {
			stm = conn.createStatement();
			strSQL = "update TAB_USUARIO set COD_PERFIL = '"+ cod_perfil2 +"'" +
					"where COD_PERFIL = '"+ cod_perfil +"'";	
		    System.out.println(strSQL);
	    	stm.execute(strSQL);
	        stm.close();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}



}

	
